<script setup lang="ts">
import { ref } from 'vue'

// 表单数据
const formData = ref({
  name: '',
  email: '',
  subject: '',
  message: ''
})

// 表单验证
const errors = ref({
  name: '',
  email: '',
  subject: '',
  message: ''
})

// 提交状态
const isSubmitting = ref(false)
const submitSuccess = ref(false)

// 验证表单
const validateForm = () => {
  let isValid = true
  errors.value = {
    name: '',
    email: '',
    subject: '',
    message: ''
  }

  if (!formData.value.name.trim()) {
    errors.value.name = '请输入您的姓名'
    isValid = false
  }

  if (!formData.value.email.trim()) {
    errors.value.email = '请输入您的邮箱'
    isValid = false
  } else if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(formData.value.email)) {
    errors.value.email = '请输入有效的邮箱地址'
    isValid = false
  }

  if (!formData.value.subject.trim()) {
    errors.value.subject = '请输入主题'
    isValid = false
  }

  if (!formData.value.message.trim()) {
    errors.value.message = '请输入您的留言'
    isValid = false
  }

  return isValid
}

// 提交表单
const submitForm = async () => {
  if (!validateForm()) return

  isSubmitting.value = true
  try {
    // 这里添加实际的表单提交逻辑
    await new Promise(resolve => setTimeout(resolve, 1000)) // 模拟API调用
    submitSuccess.value = true
    formData.value = {
      name: '',
      email: '',
      subject: '',
      message: ''
    }
  } catch (error) {
    console.error('提交失败:', error)
  } finally {
    isSubmitting.value = false
  }
}

// 重置表单
const resetForm = () => {
  formData.value = {
    name: '',
    email: '',
    subject: '',
    message: ''
  }
  errors.value = {
    name: '',
    email: '',
    subject: '',
    message: ''
  }
  submitSuccess.value = false
}
</script>

<template>
  <div class="contact">
    <!-- 页面头部 -->
    <section class="contact-header">
      <div class="container">
        <h1>联系我们</h1>
        <p>我们期待听到您的声音，随时为您提供帮助</p>
      </div>
    </section>

    <div class="container">
      <div class="contact-content">
        <!-- 联系信息 -->
        <div class="contact-info">
          <h2>联系方式</h2>
          <div class="info-items">
            <div class="info-item">
              <span class="icon">📧</span>
              <div class="info-content">
                <h3>邮箱</h3>
                <p>2693176405@qq.com</p>
              </div>
            </div>
            <div class="info-item">
              <span class="icon">📞</span>
              <div class="info-content">
                <h3>电话</h3>
                <p>18883799646</p>
              </div>
            </div>
            <div class="info-item">
              <span class="icon">🏢</span>
              <div class="info-content">
                <h3>地址</h3>
                <p>北京市朝阳区科技园区88号</p>
              </div>
            </div>
            <div class="info-item">
              <span class="icon">⏰</span>
              <div class="info-content">
                <h3>工作时间</h3>
                <p>周一至周五 9:00-18:00</p>
              </div>
            </div>
          </div>
        </div>

        <!-- 联系表单 -->
        <div class="contact-form">
          <h2>发送消息</h2>
          <form @submit.prevent="submitForm">
            <div class="form-group">
              <label for="name">姓名</label>
              <input
                type="text"
                id="name"
                v-model="formData.name"
                :class="{ error: errors.name }"
                placeholder="请输入您的姓名"
              />
              <span class="error-message" v-if="errors.name">{{ errors.name }}</span>
            </div>

            <div class="form-group">
              <label for="email">邮箱</label>
              <input
                type="email"
                id="email"
                v-model="formData.email"
                :class="{ error: errors.email }"
                placeholder="请输入您的邮箱"
              />
              <span class="error-message" v-if="errors.email">{{ errors.email }}</span>
            </div>

            <div class="form-group">
              <label for="subject">主题</label>
              <input
                type="text"
                id="subject"
                v-model="formData.subject"
                :class="{ error: errors.subject }"
                placeholder="请输入主题"
              />
              <span class="error-message" v-if="errors.subject">{{ errors.subject }}</span>
            </div>

            <div class="form-group">
              <label for="message">留言</label>
              <textarea
                id="message"
                v-model="formData.message"
                :class="{ error: errors.message }"
                placeholder="请输入您的留言"
                rows="5"
              ></textarea>
              <span class="error-message" v-if="errors.message">{{ errors.message }}</span>
            </div>

            <div class="form-actions">
              <button type="submit" class="btn-submit" :disabled="isSubmitting">
                <span v-if="isSubmitting">发送中...</span>
                <span v-else>发送消息</span>
              </button>
              <button type="button" class="btn-reset" @click="resetForm">重置</button>
            </div>
          </form>

          <!-- 提交成功提示 -->
          <div v-if="submitSuccess" class="success-message">
            <span class="success-icon">✅</span>
            <p>消息已发送，我们会尽快回复您！</p>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<style scoped>
.contact {
  min-height: 100vh;
  background: #f8fafc;
}

.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 2rem;
}

/* 页面头部样式 */
.contact-header {
  background: linear-gradient(135deg, #007bff 0%, #0056b3 100%);
  color: white;
  padding: 6rem 0;
  text-align: center;
}

.contact-header h1 {
  font-size: 3rem;
  margin-bottom: 1rem;
}

.contact-header p {
  font-size: 1.25rem;
  opacity: 0.9;
}

/* 内容区域样式 */
.contact-content {
  display: grid;
  grid-template-columns: 1fr 1.5fr;
  gap: 3rem;
  padding: 4rem 0;
}

/* 联系信息样式 */
.contact-info {
  background: white;
  border-radius: 10px;
  padding: 2rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.contact-info h2 {
  font-size: 1.8rem;
  color: #1a1a1a;
  margin-bottom: 2rem;
}

.info-items {
  display: grid;
  gap: 1.5rem;
}

.info-item {
  display: flex;
  align-items: flex-start;
  gap: 1rem;
}

.info-item .icon {
  font-size: 1.5rem;
  background: #f1f5f9;
  padding: 0.75rem;
  border-radius: 8px;
}

.info-content h3 {
  font-size: 1.1rem;
  color: #2c3e50;
  margin-bottom: 0.5rem;
}

.info-content p {
  color: #64748b;
  line-height: 1.6;
}

/* 表单样式 */
.contact-form {
  background: white;
  border-radius: 10px;
  padding: 2rem;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.contact-form h2 {
  font-size: 1.8rem;
  color: #1a1a1a;
  margin-bottom: 2rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: block;
  font-size: 1rem;
  color: #2c3e50;
  margin-bottom: 0.5rem;
}

.form-group input,
.form-group textarea {
  width: 100%;
  padding: 0.75rem 1rem;
  border: 1px solid #e2e8f0;
  border-radius: 6px;
  font-size: 1rem;
  transition: all 0.3s ease;
}

.form-group input:focus,
.form-group textarea:focus {
  outline: none;
  border-color: #3a7afe;
  box-shadow: 0 0 0 3px rgba(58, 122, 254, 0.1);
}

.form-group input.error,
.form-group textarea.error {
  border-color: #ef4444;
}

.error-message {
  color: #ef4444;
  font-size: 0.875rem;
  margin-top: 0.5rem;
}

.form-actions {
  display: flex;
  gap: 1rem;
  margin-top: 2rem;
}

.btn-submit,
.btn-reset {
  padding: 0.75rem 2rem;
  border-radius: 6px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;
  transition: all 0.3s ease;
}

.btn-submit {
  background: #3a7afe;
  color: white;
  border: none;
}

.btn-submit:hover:not(:disabled) {
  background: #2563eb;
  transform: translateY(-1px);
}

.btn-submit:disabled {
  background: #93c5fd;
  cursor: not-allowed;
}

.btn-reset {
  background: #f1f5f9;
  color: #64748b;
  border: 1px solid #e2e8f0;
}

.btn-reset:hover {
  background: #e2e8f0;
}

/* 成功提示样式 */
.success-message {
  margin-top: 2rem;
  padding: 1rem;
  background: #dcfce7;
  border-radius: 6px;
  display: flex;
  align-items: center;
  gap: 0.75rem;
}

.success-icon {
  font-size: 1.25rem;
}

.success-message p {
  color: #059669;
  font-size: 1rem;
}

/* 响应式设计 */
@media (max-width: 768px) {
  .contact-header {
    padding: 4rem 0;
  }

  .contact-header h1 {
    font-size: 2.5rem;
  }

  .contact-content {
    grid-template-columns: 1fr;
    gap: 2rem;
    padding: 2rem 0;
  }

  .form-actions {
    flex-direction: column;
  }

  .btn-submit,
  .btn-reset {
    width: 100%;
  }
}
</style> 